using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class CharacterTestFlow : MonoBehaviour
{
    public AllLevelData_SO allLevelData;
    public int difficultyDegree;
    public RandomEngine randomEngine;
    public string randomSeed;

    private void Start()
    {
        randomEngine.SetRandomSeed(randomSeed);
        randomEngine.InitOnRunStart();
        if (GetRandomLevelCharacter(difficultyDegree) != null)
        {
            Instantiate(GetRandomLevelCharacter(difficultyDegree));
            StartCoroutine(WaitOneSecondsAndStartFight());
        }
    }

    public GameObject GetRandomLevelCharacter(int degreeOfDifficulty)//根据难度获得一个随机的关卡
    {
        List<GameObject> list = new List<GameObject>();
        foreach (var item in allLevelData.levelList)
        {
            if (item.difficultyDegree == degreeOfDifficulty)
            {
                list.Add(item.characterCreater);
            }
        }
        int randomNum = RandomEngine.Instance.GetRandCombatRewards(0, list.Count);
        //Debug.Log(randomNum);
        return list[randomNum];
    }
    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.R))
        {
            SceneManager.LoadScene(SceneManager.GetActiveScene().name);//BUG 会报错显示怪物攻击动作预览问题
        }
        if (Input.GetKeyDown(KeyCode.A))//玩家攻击
        {
            if(FightController.Instance.isStartFight)
            {
                if (TurnController.Instance.isWaitForPlayer)
                {
                    /*CharacterActionData pad = new CharacterActionData(1, 0, 0, 1, 10, 2, 1);
                    PlayerFightActionHandling.Instance.DoPlayerAction(pad);
                    pad = new CharacterActionData(0, 0, 0, 2, 2, 2, 1);
                    PlayerFightActionHandling.Instance.DoPlayerAction(pad);*/
                    CharacterActionData playerActionData = new();
                    playerActionData.SingleAttackEnemy(chooseCharacterOrder:-1,actionNum:10, actionCount:2, actionTime:1);
                    StartCoroutine(CharacterActions.AttackEnemy(playerActionData));
                }
            }
            
        }
        if (Input.GetKeyDown(KeyCode.D))//玩家防御
        {
            if (FightController.Instance.isStartFight)
            {
                if (TurnController.Instance.isWaitForPlayer)
                {
                    CharacterActionData playerActionData = new();
                    playerActionData.SingleDefenceFriend(chooseCharacterOrder:0, actionNum:10, actionCount:3, actionTime:1);
                    StartCoroutine(CharacterActions.TakeDefenceForFriend(playerActionData));
                    //PlayerFightActionHandling.Instance.DoPlayerAction(pad);
                }
            }
                
        }
    }
    IEnumerator WaitOneSecondsAndStartFight()
    {
        yield return new WaitForSeconds(1f);
        FightController.Instance.StartFight();
    }
}
